/** Description of Brands.java 
 * @version 1.0
 *
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.activities;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.panther.awp.android.smoker.R;
import com.panther.awp.android.smoker.cache.Cache;
import com.panther.awp.android.smoker.database.AbstractDbAdapter;
import com.panther.awp.android.smoker.database.MyDBWrapper;

public class Brands extends ListActivity {
	
	private static final int BRAND_ACT_EDIT = 0;
	private static final int BRAND_ACT_CREATE = 1;
	private static final int EDIT_ID = Menu.FIRST;
	private static final int DEL_ID = Menu.FIRST + 1;
	private MyDBWrapper mDBWrapper;
	private ListView lv;
	private Button createBrandButton;
	private TextView numOfBrands;
	private final int INFO_DIALOG = 1001;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.brandtab);
        mDBWrapper = MyDBWrapper.getInstance(getApplicationContext());
        lv = getListView();
        registerForContextMenu(lv);
        numOfBrands = (TextView) findViewById(R.id.integer_of_brands);
        populateData();
        createBrandButton = (Button) findViewById(R.id.createBrand);
        createBrandButton.setOnClickListener(mZoomListener);
    }
	
    private void populateData() {
		Cursor cursor = mDBWrapper.getmBTDbHelper().fetchAll();
		startManagingCursor(cursor);
		BrandsCustomListAdapter brandsCustomListAdapter = new BrandsCustomListAdapter(this, cursor);
		lv.setAdapter(brandsCustomListAdapter);
		if(lv.getCount() != 1) {
			numOfBrands.setText("You have "+lv.getCount()+" brands added");
		} else {
			numOfBrands.setText("You have "+lv.getCount()+" brand added");
		}
	}
	
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, DEL_ID, 0, R.string.del_brand);
		menu.add(0, EDIT_ID, 1, R.string.edit_brand);
	}
    
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if(item.getItemId() == DEL_ID) {
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			mDBWrapper.getmBTDbHelper().delete(info.id);
			populateData();
			return true;
		} else if(item.getItemId() == EDIT_ID) {
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			Intent intent = new Intent(this, BrandEdit.class);
			intent.putExtra(AbstractDbAdapter.BT_PK_ID, info.id);
			startActivityForResult(intent, BRAND_ACT_EDIT);
			overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
		}

		return super.onContextItemSelected(item);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		//l.getAdapter().getItem(position); //TODO Make use of cursor donot requery in edit page
		Intent intent = new Intent(this, BrandEdit.class);
		intent.putExtra(AbstractDbAdapter.BT_PK_ID, id);
		startActivityForResult(intent, BRAND_ACT_EDIT);
		overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		populateData();
	}
	
	private OnClickListener mZoomListener = new OnClickListener() {
        public void onClick(View v) {
        	Intent intent = new Intent(Brands.this, BrandEdit.class);
            startActivityForResult(intent, BRAND_ACT_CREATE);
            overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
        }
    };

    private class BrandsCustomListAdapter extends CursorAdapter {
    	
    	private LayoutInflater layoutInflater;

		public BrandsCustomListAdapter(Context context, Cursor c) {
			super(context, c);
			layoutInflater = LayoutInflater.from(context);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			ImageView brandImage = (ImageView) view.findViewById(R.id.BRAND_IMAGE);
			TextView brandName = (TextView) view.findViewById(R.id.BRAND_LV_COLOUMN);
			TextView brandPrice = (TextView) view.findViewById(R.id.BRAND_LV_PRICE_COLOUMN);
			String brandUri = cursor.getString(cursor.getColumnIndexOrThrow(AbstractDbAdapter.BT_IMAGE_URI));
			if(brandUri.equals("NO_IMAGE")) {
				brandImage.setImageResource(R.drawable.icon);
			} else {
				brandImage.setImageBitmap(Cache.getCacheFile(brandUri));
			}
			brandName.setText("Brand Name : " + cursor.getString(cursor.getColumnIndexOrThrow(AbstractDbAdapter.BT_BRAND)));
			brandPrice.setText("Price Per Cigarette : " + cursor.getString(cursor.getColumnIndexOrThrow(AbstractDbAdapter.BT_BRAND_PRICE)));

		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			return layoutInflater.inflate(R.layout.brandrow, parent, false);
		}

    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.smoketabmenu, menu);
	    return true;

	}

    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		  case R.id.settings_menuItem:
					Intent intent = new Intent(this, Settings.class);
					startActivity(intent);
					return super.onOptionsItemSelected(item);
		  case R.id.info_menuItem:
				  showDialog(INFO_DIALOG);
		  default:
			  		return super.onOptionsItemSelected(item);
		  }
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder;
		AlertDialog alertDialog;
		Context mContext = Brands.this;
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
		switch (id) {
			case INFO_DIALOG:
				View layout = inflater.inflate(R.layout.info_dialog, (ViewGroup) findViewById(R.id.info_dialog_id));
				builder = new AlertDialog.Builder(mContext);
				builder.setView(layout);
				alertDialog = builder.create();
				alertDialog.setTitle("Smoker App");
				break;
			default:
				alertDialog = null;
		}
		return alertDialog;
	}
	
}
